package com.normalsys.live.service;

import com.alibaba.fastjson.JSONObject;
import com.normalsys.live.base.config.Constant;
import com.normalsys.live.base.util.HttpReqUtil;
import com.normalsys.live.base.util.string.ObjectTranslate;
import com.normalsys.live.base.util.string.StringUtil;
import com.normalsys.live.base.util.token.DateUtil;
import com.normalsys.live.bean.TemplateMsg;
import com.normalsys.live.bean.TemplateParams;
import com.normalsys.live.dao.UserDAO;
import com.normalsys.live.dao.UserFormDAO;
import com.normalsys.live.entity.User;
import com.normalsys.live.entity.UserForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 刘天宇
 * Date: 2018/8/23
 * Time: 20:13
 * Description:
 */
@Service
public class MsgService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserFormDAO userFormDAO;

    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void push() {

//        String accessToken = getAccessToken();
//        String now = DateUtil.getNowStringDate();
//        //查询所有用户设置
//        List<User> sitUsers = userDAO.getSitUsers();
//        if(!StringUtil.isEmpty(sitUsers)) {
//            for (User user: sitUsers) {
//                //查询用户表单信息
//                String formId = userFormDAO.getFormId(user.getOpenid(), now);
//                if(StringUtil.isEmpty(formId)) {
//                    continue;
//                }
//                pushMessage(user.getOpenid(), accessToken, Constant.SIT_TEMPLATE_ID, formId);
//            }
//        }
//        List<User> drinkUsers = userDAO.getDrinkUsers();
//        if(!StringUtil.isEmpty(drinkUsers)) {
//            for (User user: drinkUsers) {
//                //查询用户表单信息
//                String formId = userFormDAO.getFormId(user.getOpenid(), now);
//                if(StringUtil.isEmpty(formId)) {
//                    continue;
//                }
//                pushMessage(user.getOpenid(), accessToken, Constant.DRINK_TEMPLATE_ID, formId);
//            }
//        }

    }

    /**
     * 获得全局token
     * @return
     */
    private String getAccessToken() {
        String msg = HttpReqUtil.sendGet(Constant.ACCESS_TOKEN_URL, "grant_type=client_credential&appid=" + Constant.MINIAPPID + "&secret=" + Constant.MINISECRET);
        if(StringUtil.isEmpty(msg)) {
            return null;
        }

        return ObjectTranslate.getString(JSONObject.toJavaObject(JSONObject.parseObject(msg),Map.class).get("access_token"));
    }

    /**
     *
     * @param userid
     * @param access_token
     * @param formId
     * @param templateId
     * @return
     */
    private void pushMessage(String userid, String access_token, String formId, String templateId) {
        String data = formatData(userid, templateId, formId);

        String sendMsg = HttpReqUtil.sendPost(Constant.SEND_MSG_URL + access_token, data);
        System.out.println(sendMsg);
    }

    /**
     *
     * @param touser
     * @param template_id
     * @param form_id
     * @return
     */
    private String formatData(String touser, String template_id, String form_id) {
        Map<String, TemplateParams> data = new HashMap<>();
        data.put("keyword1", new TemplateParams("1", "#173177"));
        data.put("keyword2", new TemplateParams("1", "#173177"));
        data.put("keyword3", new TemplateParams("商品即将过期，请尽快使用", "#173177"));

        TemplateMsg template = new TemplateMsg(touser, template_id, form_id, data, "pages/index/index?push=true");

        return JSONObject.toJSONString(template);
    }


}
